﻿
using lhservice.common.websocket ;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using FrameWork;
using Microsoft.AspNetCore.Authorization;
using System.Net.WebSockets;
using lhservice.Models;

namespace lhservice.Controllers
{
  /// <summary>
  /// websocket 服务
  /// </summary>
  [Authorize]
  [ApiController]
  [Route("api/[controller]")]
  [AllowAnonymous]  
  public class WsManageController : TimController
  {    
    [HttpPost("[action]")]
    public async Task<IActionResult> Push([FromForm] PushTarget model)
    {     
      if (model.clientlis.Count > 0) {
        var clientLis = WebsocketClientCollection.GetClientList(model.clientlis);
        await WebsocketClientCollection.SendMessageToClientList(clientLis,"push", model.msg);       
      }
      else {
        var cc = WebsocketClientCollection.GetClientList(model.lineid);
        await WebsocketClientCollection.SendMessageToClientList(cc,"push", model.msg);
      }        
      return await PublicTool.ReturnSuccess("push over!");
    }

    [HttpGet("[action]")]
    public async Task<IActionResult> GetClientList()
    {
      var clientLis = WebsocketClientCollection.GetClientList();     
      return await PublicTool.ReturnSuccess(clientLis);
    }
  }
}
